var eModal = $('#modal'), eModal2 = $('#modal2'), eModalBtn = $('#modalOk'), eDescZone = $('#descZone');
var eResult = $('#result'), eResultMsg = $('#resultMsg'), eNewText = $('#newText'), bChange = false;
var newText = eNewText.val();
$('#compareBtn3').on('click', function(e){
    e.preventDefault();
    eModal2.css('zIndex', 0);
});
$('#agreeBtn').on('click', function(e){
    e.preventDefault();
    eResult.val(1);
    eResultMsg.html('<span class="green">如果您确认通过'+author+'的修改，文档将会更新为修改后的版本...</span>');
    eModal.removeClass('hide').addClass('modal-show');
    window.setTimeout(function(){
        eModal.addClass('in');
    },20);
    eDescZone.val('您的提交已经通过审核，对此表示十分感谢，愿您每天好心情！').focus();
});
$('#noagreeBtn').on('click', function(e){
    e.preventDefault();
    eResult.val(2);
    eResultMsg.html('<span class="red">如果您确认拒绝'+author+'的修改，则'+author+'的修改内容将会无效...</span>');
    eModal.removeClass('hide').addClass('modal-show');
    window.setTimeout(function(){
        eModal.addClass('in');
    },20);
    eDescZone.val('十分抱歉，您提交的修改未能通过审核，希望您不要放弃！').focus();
});
// 关闭modal
eModal.on('click', '#modalClose,#modalNo', function(){
    eModal.removeClass('modal-show').removeClass('in');
    window.setTimeout(function(){
        eModal.addClass('hide');
    }, 1000);
});
eDescZone.blur(function(){
    if($(this).val() && $(this).hasClass('error')) $(this).removeClass('error');
});
eModalBtn.on('click', function(e){
    e.preventDefault();
    var desc = eDescZone.val();
    if(desc == ''){
        eDescZone.addClass('error');
        return ;
    }
    $('#desc').val(eDescZone.val());
    $('#md').val(bChange ? eNewText.val() : '');
    $('#html').val(eResult.val() == 1 ? editor.getHTML() : '');
    $('#editForm').submit();
});
var editor;
$(function() {
    editor = editormd("editormd", {     // 编辑器ID
        width   : "100%",       // 编辑器宽度
        height  : "100%",       // 编辑器高度
        path    : editorPath,
        toolbarIcons : function() {
            return ["editNo", "undo", "redo", "|", "bold", "del", "italic", "quote", "|", "h1", "h2", "h3", "|", "list-ul", "list-ol", "hr", "|", "link", "image", "code", "preformatted-text", "code-block", "table", "|", "watch", "preview", "search", "|", "help", "editOk"]
        },
        toolbarCustomIcons : {
            editOk   : '<a href="javascript:void(0);" title="保存编辑" unselectable="on"><i class="fa green" name="editOk" unselectable="on" onclick="saveEdit(0);">保存</i></a>',
            editNo : '<a href="javascript:void(0); "title="取消编辑" unselectable="on"><i class="fa red" name="editNo" unselectable="on" onclick="saveEdit(-1);">取消</i></a>'
        },
        saveHTMLToTextarea : true,
        emoji : false,
        taskList : false,
        tocm : false,         // Using [TOCM]
        imageUpload : true
    });
});
function saveEdit(type){
    if(type == -1){
        editor.setMarkdown(newText);
    }else if(type == 0){
        bChange = true;
        newText = eNewText.val();
        diffUsing($('#compareBtn1'), 0);
    }
    eModal2.css('zIndex', -1);
}